package com.github.abdularis.sample.slice;

import com.github.abdularis.buttonprogress.DownloadButtonProgress;
import com.github.abdularis.sample.ResourceTable;
import com.github.abdularis.sample.SampleTask;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        DownloadButtonProgress buttonProgress1 = (DownloadButtonProgress) findComponentById(ResourceTable.Id_button_progress_1);
        DownloadButtonProgress buttonProgress2 = (DownloadButtonProgress) findComponentById(ResourceTable.Id_button_progress_2);

        Text subtitle_1 = (Text) findComponentById(ResourceTable.Id_subtitle_1);
        Text subtitle_2 = (Text) findComponentById(ResourceTable.Id_subtitle_2);

        setListenerFor(buttonProgress1);
        setListenerFor(buttonProgress2);

        buttonProgress1.addOnStateChangedListeners(newState -> {
            if (newState == DownloadButtonProgress.STATE_FINISHED) {
                buttonProgress1.setVisibility(Component.HIDE);
            }

            switch (newState){
                case DownloadButtonProgress.STATE_IDLE:
                    subtitle_1.setText("Idle");
                    break;
                case DownloadButtonProgress.STATE_INDETERMINATE:
                    subtitle_1.setText("Indeterminate");
                    break;
                case DownloadButtonProgress.STATE_DETERMINATE:
                    subtitle_1.setText("Determinate");
                    break;
                case DownloadButtonProgress.STATE_FINISHED:
                    subtitle_1.setText("Finished");
                    break;
                default:
                    subtitle_1.setText("");
                    break;
            }
        });

        buttonProgress2.addOnStateChangedListeners(newState -> {
            switch (newState){
                case DownloadButtonProgress.STATE_IDLE:
                    subtitle_2.setText("Idle");
                    break;
                case DownloadButtonProgress.STATE_INDETERMINATE:
                    subtitle_2.setText("Indeterminate");
                    break;
                case DownloadButtonProgress.STATE_DETERMINATE:
                    subtitle_2.setText("Determinate");
                    break;
                case DownloadButtonProgress.STATE_FINISHED:
                    subtitle_2.setText("Finished");
                    break;
                default:
                    subtitle_2.setText("");
                    break;
            }
        });
    }

    private void setListenerFor(DownloadButtonProgress downloadButtonProgress) {
        downloadButtonProgress.addOnClickListener(new DownloadButtonProgress.OnClickListener() {
            @Override
            public void onIdleButtonClick(Component view) {
                new Thread(new SampleTask(new EventHandler(EventRunner.getMainEventRunner()), downloadButtonProgress)).start();
            }

            @Override
            public void onCancelButtonClick(Component view) {
                // called when cancel button/icon is clicked
            }

            @Override
            public void onFinishButtonClick(Component view) {
                // called when finish button/icon is clicked
            }
        });
    }
}
